package com.flute.icrawler.check.checker;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class ObjCompare implements ICompare {

	@Override
	public boolean compare(File targetFile, File sourceFile) {
		CompareObj o1 = createXmlObj(targetFile);
		CompareObj o2 = createXmlObj(sourceFile);
		if(o1 == null || o2 == null){
			return false;
		}
		return o1.equals(o2);
	}
	@SuppressWarnings({ "unchecked" })
	private CompareObj createXmlObj(File file){
        SAXBuilder builder = new SAXBuilder();
		CompareObj compareobj = new  CompareObj();
		Document doc = null;
		try {
			doc = builder.build(file);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Element targetroot = doc.getRootElement();
		
		Element url = targetroot.getChild("url");
		compareobj.setUrlid(url.getAttributeValue("id"));
		Element fileName = targetroot.getChild("fileName");
		compareobj.setFileNameid(fileName.getAttributeValue("id"));
		List<Element> hrefList = fileName.getChildren("href");
		List<String> list = new ArrayList<String>();
		for(Element href : hrefList){
			list.add(href.getText());
		}
		compareobj.setHrefList(list);
		return compareobj;
	}

}
